package com.djs.redis.impl;

import com.djs.redis.JedisConfig;
import com.djs.redis.IJedisOperator;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @Description
 * @auther dengjingsi
 * @create 2020-03-15 23:35
 */
public class JedisOperator implements IJedisOperator {
    /**
     * jedis配置参数
     */
    private JedisConfig jedisConfig;
    /**
     * 线程池
     */
    private JedisPool jedisPool;
    /**
     * 线程池配置类
     */
    private GenericObjectPoolConfig jedisPoolConfig;
    @Override
    public void init() {
        if(null != jedisPool){
            return;
        }
        if(null == jedisPoolConfig) {
            jedisPoolConfig = new JedisPoolConfig();
        }
        if(null == jedisConfig){
            jedisConfig = new JedisConfig();
        }
        jedisPool = new JedisPool(jedisPoolConfig, jedisConfig.getHost(), jedisConfig.getPort(), jedisConfig.getTimeout(), jedisConfig.getPassword());
    }

    @Override
    public Jedis getJedis() {
        return jedisPool.getResource();
    }

    @Override
    public void destroy() {
        jedisPool.destroy();
    }

    public void setJedisConfig(JedisConfig jedisConfig) {
        this.jedisConfig = jedisConfig;
    }

    public void setJedisPool(JedisPool jedisPool) {
        this.jedisPool = jedisPool;
    }
}
